package com.ntv.presentation.spring.web.backoffice;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.ntv.domain.INews;
import com.ntv.domain.IUser;
import com.ntv.domain.Login;
import com.ntv.service.DomainException;
import com.ntv.service.news.INewsAppService;

@Controller
@RequestMapping("/list_news.html")
@SessionAttributes("userSession")
public class NewsListingController {

	private static final Logger logger = Logger.getLogger(NewsListingController.class);
	private static final String MA_LOGIN= "login";

	@Autowired
	private INewsAppService newsAppService;

	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView newForm(Model argModle, 
									@ModelAttribute(MA_LOGIN) Login  login, 
									HttpServletRequest request)
	{

		ModelAndView mNv = null;
		IUser user = (IUser)request.getSession().getAttribute("userSession");

		if (user == null) {
			login.setTarget("list_news.html");
			return new ModelAndView("signin");
		}

		try {
			List<INews> newsList = newsAppService.getNews();
			mNv = new ModelAndView("admin.news.list","newsList",newsList);
		} catch (DomainException e) {
			e.printStackTrace();
			logger.error(e);
			mNv = new ModelAndView("/backoffice.html");
		}
		return mNv;

	}
}//:~

